package com.pxst.annotations.validated;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.ValidationException;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author rj
 * @create 2023-09-25-16:53
 */
@Slf4j
public class DecimalValidated implements ConstraintValidator<DecimalValidator, List<BigDecimal>> {

    @Override
    public boolean isValid(List<BigDecimal> value, ConstraintValidatorContext context) {
        if (value.get(0).compareTo(BigDecimal.ZERO) < 0 || value.get(1).compareTo(BigDecimal.ZERO) < 0){
            throw new ValidationException("输入的金额必须大于0");
        }
        if (value.size() < 2){
            throw new ValidationException("传入的数量不能少于2");
        }
        // 判断最小值是否大于最大值
        if (value.get(0).compareTo(value.get(1)) > 0){
            return false;
        }
        return true;
    }
}
